LINE_TITLE = ['行号','执行次数','总耗时','单次耗时','占比', '代码']
LINE_TITLE_STR = '\t'.join(LINE_TITLE) + '\n'
class Chunk(object):
    """代码数据块"""

    def __init__(self) -> None:
        self.total_time = 0
        self.file_path = ''
        self.location = ''
        self.detail_lines = []
        self.sentence_max_consuming_time = 0
        self.sentence_max_time = 0

    def copy(self):
        """复制一份对象"""
        c = Chunk()
        c.total_time = self.total_time
        c.file_path = self.file_path
        c.location = self.location
        c.detail_lines = self.detail_lines
        c.sentence_max_consuming_time = self.sentence_max_consuming_time
        c.sentence_max_time = self.sentence_max_time
        return c
    
    def to_string(self):
        """变回字符串"""
        res_str1 = f'Total time: {self.total_time}s\nFile:{self.file_path}\nFunction:{self.location}\n'
        tostring = lambda a: str(a)
        get_line = lambda arr: '\t'.join(map(tostring, arr))
        res_str2 = '\n'.join(map(get_line, self.detail_lines))
        return f'{res_str1}{LINE_TITLE_STR}{res_str2}'

    def set_total_time(self, total_time):
        try:
            self.total_time = float(total_time)
        except Exception as err:
            self.total_time = 0
    
    def set_sentence_max_consuming_time(self, sentence_max_consuming_time):
        try:
            self.sentence_max_consuming_time = float(sentence_max_consuming_time)
        except Exception as err:
            self.sentence_max_consuming_time = 0

    def set_sentence_max_time(self, sentence_max_time):
        try:
            self.sentence_max_time = float(sentence_max_time)
        except Exception as err:
            self.sentence_max_time = 0


    def set_file_path(self, file_path):
        self.file_path = file_path

    def set_location(self, location):
        self.location = location
    
    def set_detail_lines(self, detail_lines):
        self.detail_lines = detail_lines

    def add_detail_line(self, line):
        self.detail_lines.append(line)

    def clear_detail_line(self):
        self.detail_lines = []